package com.hanxiang.hankol;


import android.app.Application;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baselibrary.net.HttpHelper;
import com.baselibrary.net.interceptor.HeaderInterceptor;
import com.baselibrary.net.interceptor.HttpStatusInterceptor;
import com.baselibrary.net.interceptor.LoggingInterceptor;
import com.baselibrary.utils.Config;
import com.baselibrary.utils.ContextUtils;
import com.baselibrary.view.refresh_header.HKRefreshHeaderView;
import com.hanxiang.hankol.utils.AppUtils;
import com.okhttp.utils.HttpsUtils;
import com.okhttp.utils.OkHttpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.tencent.rtmp.TXLiveBase;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
//import okhttp3.logging.HttpLoggingInterceptor;

public class MyApplication extends Application {

    public static String licenceUrl = "http://license.vod2.myqcloud.com/license/v1/625ed6f99ef40bc78f4f2ca57f907b02/TXUgcSDK.licence";
    public static String licenseKey = "6055addd9bd69f924b1fd349404d9ba0";

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new HKRefreshHeaderView(context));
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    @Override
    public void onCreate() {
        ARouter.init(this);
        ARouter.openDebug();
        initOkHttp(this);
        TXLiveBase.getInstance().setLicence(this, licenceUrl, licenseKey);
        UMConfigure.preInit(this, Config.UMENG_APP_KEY, "hanke");
        UMConfigure.init(this, Config.UMENG_APP_KEY, "hanke",
                UMConfigure.DEVICE_TYPE_PHONE, Config.UMENG_MESSAGE_SECRE);
        UMConfigure.setLogEnabled(true);
        String FileProvider = "com.hanxiang.hankol.FileProvider";
        PlatformConfig.setWeixin("wxfed2752ae7edf52a", "6d9f88623ee42b57fb8d05099f5779a3");
        PlatformConfig.setWXFileProvider(FileProvider);
        super.onCreate();
        AppUtils.initSystemDic();
    }

    private void initOkHttp(Application application) {
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(20000L, TimeUnit.MILLISECONDS).readTimeout(20000L, TimeUnit.MILLISECONDS).addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY)).hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }).sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).build();

        OkHttpUtils.initClient(okHttpClient);

        //网络请求日志
        LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
        HeaderInterceptor headerInterceptor = new HeaderInterceptor();
        HttpStatusInterceptor httpStatusInterceptor = new HttpStatusInterceptor();
        HttpHelper.getInstance().init(application.getApplicationContext(), headerInterceptor, loggingInterceptor, httpStatusInterceptor);
    }

}
